import java.util.*;

public class Manager {
    static String loginID = "primary";
    static String loginPassword = "111111";
    public static List<Customer> customers = new ArrayList<>();
    static List<Product> products = new ArrayList<>();
    static Scanner scanner = new Scanner(System.in);
    private static Manager instance;

    public Manager() {
        addInitialCustomers();
        addInitialProducts();
    }

    public static Manager getInstance() {
        if (instance == null) {
            instance = new Manager();
        }
        return instance;
    }

    private void addInitialCustomers() {
        addCustomer(new Customer("ID001", "张三", "VIP", "2022-01-01", 3000.0, "13800138000", "zhangsan@qq.com", "password1"));
        addCustomer(new Customer("ID002", "李四", "普通", "2022-02-01", 500.0, "13900139000", "lisi@qq.com", "password2"));
    }

    private void addInitialProducts() {
        addProduct(new Product("id001", "电视", "三星", "2022-01-01", "LED", 500.0, 600.0, 10));
        addProduct(new Product("id002", "手机", "苹果", "2022-02-01", "Iphone 12", 800.0, 900.0, 20));
    }

    public static void addCustomer(Customer customer) {
        customers.add(customer);
    }

    public static void addProduct(Product product) {
        products.add(product);
    }

    public void changeAdminPassword() {
        System.out.println("请输入新密码");
        loginPassword = scanner.next();
    }

    public void resetUserPassword(Customer customer, String newPassword) {
        customer.changePassword(newPassword);
    }

    public static void login() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入登录账号");
            String loginID1 = scanner.next();
            System.out.println("请输入登录密码");
            String loginPassword1 = scanner.next();
            if (Objects.equals(loginID1, loginID) && Objects.equals(loginPassword1, loginPassword)) {
                System.out.println("登录成功");
                break;
            } else {
                System.out.println("匹配失败，请重新输入");
            }
        }
    }

    public void listAllCustomers() {
        for (Customer customer : customers) {
            System.out.println("ID: " + customer.getID() + ", Name: " + customer.getName() + ", Level: " +
                    customer.getLevel() + ", Registered: " + customer.getRegisteredDate() + ", Total Spent: " +
                    customer.getTotalSpent() + ", Phone: " + customer.getPhone() + ", Email: " + customer.getEmail());
        }
    }

    public void deleteCustomer(String customerId) {
        customers.removeIf(customer -> customer.getID().equals(customerId));
        System.out.println("删除成功");
    }

    public void searchCustomer(String query) {
        boolean found = false;
        for (Customer customer : customers) {
            if (customer.getID().equals(query) || customer.getName().equalsIgnoreCase(query)) {
                System.out.println("ID: " + customer.getID() + ", Name: " + customer.getName() + ", Level: " +
                        customer.getLevel() + ", Registered: " + customer.getRegisteredDate() + ", Total Spent: " +
                        customer.getTotalSpent() + ", Phone: " + customer.getPhone() + ", Email: " + customer.getEmail());
                found = true;
            }
        }
        if (!found) {
            System.out.println("没有找到所给出ID的用户");
        }
    }

    public Customer getCustomerById(String customerId) {
        for (Customer customer : customers) {
            if (customer.getID().equals(customerId)) {
                return customer;
            }
        }
        return null;
    }

    public static void listAllProducts() {
        for (Product product : products) {
            System.out.println("ProductId: " + product.getProductId() + ", ProductName: " + product.getProductName() +
                    ", Manufacturer: " + product.getManufacturer() + ", ProductionDate: " + product.getProductionDate() +
                    ", Model: " + product.getModel() + ", PurchasePrice: " + product.getPurchasePrice() +
                    ", RetailPrice: " + product.getRetailPrice() + ", Quantity: " + product.getQuantity());
        }
    }

    public static void updateProduct(String productId, Product updatedProduct) {
        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getProductId().equals(productId)) {
                products.set(i, updatedProduct);
                return;
            }
        }
    }

    public static void deleteProduct(String productId) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("删除后无法恢复，请用户确认是否继续删除操作（输入yes或no）：");
        String input = scanner.nextLine();
        if (input.equalsIgnoreCase("yes")) {
            products.removeIf(product -> product.getProductId().equals(productId));
            System.out.println("商品已删除");
        } else {
            System.out.println("取消删除操作");
        }
    }
}